package com.guoqing.example.streams;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author zhangguoqing
 * @date 2020/9/25
 * Description: TODO
 */
public class StreamMap {

    /**
     * 将 Map 根据 key 排序 升序
     * @param map 入参
     * @param <K> key
     * @param <V> value
     * @return return
     */
    public static <K extends Comparable<? super K>, V> Map<K, V> sortMap(Map<K, V> map) {
        Map<K, V> linkedHashMap = new LinkedHashMap<>();
        map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey().reversed())
                .forEachOrdered(e -> linkedHashMap.put(e.getKey(), e.getValue()));
        return linkedHashMap;
    }


    public static void main(String[] args) {
        User user = new User();
        user.setId("111");
        User user2 = new User();
        user2.setId("222");
        User user3 = new User();
        user3.setId("333");
        Map<String, User> map = new HashMap<>();
        map.put("2", user);
        map.put("zhang", user2);
        map.put("a", user3);


        System.out.println(map);
        System.out.println(sortMap(map));
    }
}
